/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package application.tools;

import application.model.DistanceCalcSettings;
import application.model.EntityAccomplishmentReport;
import application.model.EntitySearchResults;
import application.model.GraphEntity;
import application.model.GraphEntityAccomps;
import application.model.GraphEntityKeywords;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 *
 * @author aa6345
 */
public class DistanceCalcTools {

    //EntityID, GraphEntity
    HashMap<Long, GraphEntity> searchResults2ge = new HashMap<Long, GraphEntity>();
    //EntityID, GraphEntityKeywords
    HashMap<Long, GraphEntityKeywords> searchResults2geks = new HashMap<Long, GraphEntityKeywords>();
    //EntityID, GraphEntityAccomps
    HashMap<Long, GraphEntityAccomps> searchResults2geas = new HashMap<Long, GraphEntityAccomps>();
    
    List<DistanceCalcSettings> distanceCalcSettingsList;

    public List<DistanceCalcSettings> getDistanceCalcSettingsList() {
        return distanceCalcSettingsList;
    }

    public void setDistanceCalcSettingsList(List<DistanceCalcSettings> distanceCalcSettingsList) {
        this.distanceCalcSettingsList = distanceCalcSettingsList;
    }

    public HashMap<Long, GraphEntityAccomps> getSearchResults2geas() {
        return searchResults2geas;
    }

    public void setSearchResults2geas(HashMap<Long, GraphEntityAccomps> searchResults2geas) {
        this.searchResults2geas = searchResults2geas;
    }

    public HashMap<Long, GraphEntityKeywords> getSearchResults2geks() {
        return searchResults2geks;
    }

    public void setSearchResults2geks(HashMap<Long, GraphEntityKeywords> searchResults2geks) {
        this.searchResults2geks = searchResults2geks;
    }

    public HashMap<Long, GraphEntity> getSearchResults2ge() {
        return this.searchResults2ge;
    }

    public void setSearchResults2ge(HashMap<Long, GraphEntity> searchResults2graphEntity) {
        this.searchResults2ge = searchResults2graphEntity;
    }

    public double calcWeightTypeDegree(EntitySearchResults entSearchRes, String searchTerm, String searchOperator) {
        double weight = 0.0;
        String searchStringClean;
//System.out.println(">>>>>>> The searchTerm is: "+searchTerm);
        //add type of match weight
        weight = weight + entSearchRes.getAccompTypeWeight();

        //if search operator is NOT then search for first key term only
        if(searchOperator != null) {
            String[] searchTerms = searchTerm.toLowerCase().split(" "+searchOperator.toLowerCase()+" ");
            if(searchOperator.toLowerCase().trim().equals("not")) {
                searchStringClean = searchTerms[0];
            } else if(searchOperator.toLowerCase().trim().equals("and")) {
                searchStringClean = searchTerms[0] + " " + searchTerms[1];
            }
            else {
                searchStringClean = searchTerm;
            }
        } else {
            searchStringClean = searchTerm;
        }
//System.out.println(">>>>>>> The searchTermClean is: "+searchStringClean);
        //add degree of match weight
        if(entSearchRes.getAccompTitle().length() == searchStringClean.length()) {

            if(searchTerm.contains(" ")) {
                //multiply by 4 if searchTerm made of more than one word
                //if(entSearchRes.getEntityID() == 347) { System.out.println("********* timed by four "+weight); }
                weight = weight * distanceCalcSettingsList.get(0).getDegreeOfMatchWeightA();
            } else {
                //multiply by 2 if searchTerm made of one word
                //if(entSearchRes.getEntityID() == 347) { System.out.println("********* timed by two "+weight); }
                weight = weight * distanceCalcSettingsList.get(0).getDegreeOfMatchWeightB();
            }

        }  else if (searchStringClean.length() >= (entSearchRes.getAccompTitle().length()/2)) {
            //multiply by 2 if searchTerm length is 50% or more of title lenght
            //if(entSearchRes.getEntityID() == 347) { System.out.println("********* timed by two "+weight); }
            weight = weight * distanceCalcSettingsList.get(0).getDegreeOfMatchWeightB();
        } else {
            //multiply by 1
            //if(entSearchRes.getEntityID() == 347) { System.out.println("********* timed by one "+weight); }
            weight = weight * distanceCalcSettingsList.get(0).getDegreeOfMatchWeightC();
        }
        return weight;
    }

    public void typeAndDegreeOfMatchScores(List<EntitySearchResults> entitySearchResults, String searchTerm) {

        for(EntitySearchResults entSearchRes : entitySearchResults) {
//            if(this.getSearchResults2ge().containsKey(entSearchRes.getEntityID())) {
                //System.out.println("!!!!! Id found in GraphEntity: "+entSearchRes.getEntityID());
                if(this.getSearchResults2geks().containsKey(entSearchRes.getEntityID())) {
                    //System.out.println("!!!!! Id found in GraphEntityKeywords: "+entSearchRes.getEntityID());
                    //check if current searchTerm exists
                    if(this.getSearchResults2geks().get(entSearchRes.getEntityID()).getGek().containsKey(searchTerm)) {
                        //add entityAccomplishmentID to current list for existing keyword
                        /*
                        System.out.println("MMMMMMMM"+this.getSearchResults2geks().get(entSearchRes.getEntityID()).getGek().containsKey(searchTerm));
                        System.out.println("MMMMMMMM"+entSearchRes.getEntityAccomplishmentID());
                        System.out.println("MMMMMMMM"+searchTerm);
                        System.out.println("MMMMMMMM"+this.getSearchResults2geks().get(entSearchRes.getEntityID()).getGek().toString());
*/
                        this.getSearchResults2geks().get(entSearchRes.getEntityID()).getGek().get(searchTerm).add(entSearchRes.getEntityAccomplishmentID());
                    } else {
                        //add entityAccomplishmentID to a new list for new keyword
                        ArrayList<Long> listOfEntIDs = new ArrayList<Long>();
                        listOfEntIDs.add(entSearchRes.getEntityAccomplishmentID());
                        this.getSearchResults2geks().get(entSearchRes.getEntityID()).getGek().put(searchTerm, listOfEntIDs);
                    }
                } else {
                    System.out.println("!!!!! Error, Id NOT found in GEKS but exists in GE: "+entSearchRes.getEntityID());
                }

                if(this.getSearchResults2geas().containsKey(entSearchRes.getEntityID())) {
                    //System.out.println("!!!!! Id found in GraphEntityAccomplishments: "+entSearchRes.getEntityID());
                    //check if current entityAccomplishment exists
                    if(this.getSearchResults2geas().get(entSearchRes.getEntityID()).getGea().containsKey(entSearchRes.getEntityAccomplishmentID())) {
                        //DO Nothing in entityAccomplishment details to current list for existing accomplishments

                    } else {
                        //add entityAccomplishment details with id as key

                        EntityAccomplishmentReport ear = new EntityAccomplishmentReport();

                        ear.setAccompID(entSearchRes.getEntityAccomplishmentID());
                        ear.setAccompTitle(entSearchRes.getAccompTitle());
                        ear.setAccomplishmentType(entSearchRes.getAccompTypeDescription());
                        //ear.setAccomplishmentWeight(entSearchRes.getAccompPriority());

                        this.getSearchResults2geas().get(entSearchRes.getEntityID()).getGea().put(entSearchRes.getEntityAccomplishmentID(), ear);
                    }

                } else {
                    System.out.println("!!!!! Error, Id NOT found in GEAS but exists in GE: "+entSearchRes.getEntityID());
                }
/*
            } else {
                //create new GraphEntity
                GraphEntity ge = new GraphEntity();
                ge.setEntityActive(entSearchRes.getEntityActive());
                ge.setEntityEmail(entSearchRes.getEntityEmail());
                ge.setEntityTypeDescription(entSearchRes.getEntityTypeDescription());
                ge.setEntityFName(entSearchRes.getEntityFname());
                ge.setEntityMName(entSearchRes.getEntityMname());
                ge.setEntitySName(entSearchRes.getEntitySname());
                ge.setTotalWeight("0");
                ge.setEntityContactInfo("");
                //add GraphEntity to the main list of entity details
                this.searchResults2ge.put(entSearchRes.getEntityID(), ge);
                //System.out.println("!!!!! Id NOT found in GraphEntity therefore Added: "+entSearchRes.getEntityID());

                //create new keyword entry if required
                if(this.getSearchResults2geks().containsKey(entSearchRes.getEntityID())) {
                    System.out.println("!!!!! Error, Id found in GEKS but Not in GE: "+entSearchRes.getEntityID());
                } else {
                    GraphEntityKeywords geks = new GraphEntityKeywords();
                    ArrayList<Long> entAccompIdList = new ArrayList<Long>();
                    HashMap<String,ArrayList<Long>> kwrdList = new HashMap<String,ArrayList<Long>>();
                    entAccompIdList.add(entSearchRes.getEntityAccomplishmentID());
                    kwrdList.put(searchTerm, entAccompIdList);
                    geks.setGek(kwrdList);
                    this.searchResults2geks.put(entSearchRes.getEntityID(),geks);
                    //System.out.println("!!!!! Id NOT found in GraphEntityKeywords therefore Added: "+entSearchRes.getEntityID());
                }

                //create new acommplishment entry if required
                if(this.getSearchResults2geas().containsKey(entSearchRes.getEntityID())) {
                    System.out.println("!!!!! Error, Id found in GEAS but Not in GE: "+entSearchRes.getEntityID());
                } else {
                    GraphEntityAccomps geas = new GraphEntityAccomps();
                    EntityAccomplishmentReport ear = new EntityAccomplishmentReport();

                    ear.setAccompID(entSearchRes.getEntityAccomplishmentID());
                    ear.setAccompTitle(entSearchRes.getAccompTitle());
                    ear.setAccomplishmentType(entSearchRes.getAccompTypeDescription());
                    ear.setAccomplishmentWeight(entSearchRes.getAccompPriority());

                    geas.getGea().put(entSearchRes.getEntityAccomplishmentID(), ear);

                    this.searchResults2geas.put(entSearchRes.getEntityID(), geas);
                    //System.out.println("!!!!! Id NOT found in GraphEntityAccomps therefore Added: "+entSearchRes.getEntityID());
                }

            }
            */
        }

    }

}
